package com.fyzk.activity.prize;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.prize.detail.PrizeDetailActivity;
import com.fyzk.adapter.PrizeHistoryAdapter;
import com.fyzk.bean.HistoryPrizeResponse;
import com.fyzk.bean.PrizeHistoryItem;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.dialog.DJ_ProgressDialog;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.layoutmanagers.ScrollSmoothLineaerLayoutManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * 数字彩历史开奖
 */
public class NumberLotteryPrizeHistroyActivity extends BaseActivity implements
        onTopTitleClickListener, PrizeHistoryAdapter.OnItemClickListener {

    private TopTitleView mVi_topView;
    private UltimateRecyclerView mLVi_historyLottery;

    private PrizeHistoryAdapter mAdpt_PrizeHistoryAdapter;

    private ArrayList<PrizeHistoryItem> mArr_HistoryItems = new ArrayList<>();
    private int lotteryId;
    private LinearLayoutManager linearLayoutManager;
//    private ItemTouchHelper mItemTouchHelper;

    private int pageCount = 20;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dj_activity_historyprize);
        lotteryId = getIntent().getIntExtra(IntentParamUtils.LOTTERYID, 0);
        initView();
        DJ_ProgressDialog.show(getContext(), true, true);
        requestHistoryInfoList();
    }

    private boolean isLoadMore = false;

    private int pageindex = 1;

    ScrollSmoothLineaerLayoutManager mgm;

    private void initView() {
        mVi_topView = (TopTitleView) findViewById(R.id.topTitle);
        mVi_topView.setOnTitleClickListener(this);
        mVi_topView.setCenterText(AppLotteryInfos.getLotteryName(lotteryId) + "历史开奖");
        mVi_topView.setRightVisible(View.INVISIBLE);
        mLVi_historyLottery = (UltimateRecyclerView) findViewById(R.id.activity_prizehistory_list);
        mLVi_historyLottery.setHasFixedSize(false);
        mgm = new ScrollSmoothLineaerLayoutManager(this,
                LinearLayoutManager.VERTICAL, false, 300);
        mLVi_historyLottery.setLayoutManager(mgm);

        mLVi_historyLottery.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageindex = 1;
                        enableLoadMore();
                        requestHistoryInfoList();
                    }
                }, 1000);
            }
        });


        mAdpt_PrizeHistoryAdapter = new PrizeHistoryAdapter(this, mArr_HistoryItems);
        mAdpt_PrizeHistoryAdapter.setOnItemClickListener(this);
        enableEmptyViewPolicy();
        enableLoadMore();
        mLVi_historyLottery.setAdapter(mAdpt_PrizeHistoryAdapter);

    }


    protected void enableEmptyViewPolicy() {
        mLVi_historyLottery.setEmptyView(R.layout.empty_view, UltimateRecyclerView
                .EMPTY_SHOW_LOADMORE_ONLY);
    }

    private void enableLoadMore() {
        mLVi_historyLottery.reenableLoadmore();
        mLVi_historyLottery.setLoadMoreView(R.layout.custom_bottom_progressbar);
        mLVi_historyLottery.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount >= pageindex * pageCount) {
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        public void run() {
                            isLoadMore = true;
                            pageindex++;
                            requestHistoryInfoList();
                        }
                    }, 1000);
                }
            }
        });
    }

    private void requestHistoryInfoList() {
        try {
            JSONObject params = new JSONObject();
            params.put("lotteryType", lotteryId);
            params.put("pageIndex", pageindex);
            params.put("maxResult", pageCount);
            httpPostJson(CommandUtil.HISTORY_LOTTERY, Commands.WINCODEHISTORY, params, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        if (resultBean.getErrorcode().equals("0")) {
            HistoryPrizeResponse info = JSON.parseObject(resultBean.getData(),
                    HistoryPrizeResponse.class);
            LogUtil.d("size:" + info.wincodes.size());
            if (pageCount > info.wincodes.size()) {//当返回的子项数小于页数
                mLVi_historyLottery.disableLoadmore();
            }
            if (isLoadMore) {
                isLoadMore = false;
                mAdpt_PrizeHistoryAdapter.insert(info.wincodes);
            } else {
                this.mArr_HistoryItems.clear();
                this.mArr_HistoryItems.addAll(info.wincodes);
                mAdpt_PrizeHistoryAdapter.notifyDataSetChanged();

            }
            if (mArr_HistoryItems.size() == 0) {
                AppToast.getToast().show(R.string.text_norecord);
            }
        } else {
            if (isLoadMore) {
                pageindex--;
                mLVi_historyLottery.disableLoadmore();
            }
            showToast(resultBean.getMessage());
        }
    }

    @Override
    protected void onFailure(int requestCode, String erro, String message) {
        isLoadMore = false;
        pageindex--;
        mLVi_historyLottery.setRefreshing(false);
        mLVi_historyLottery.disableLoadmore();
        mAdpt_PrizeHistoryAdapter.getCustomLoadMoreView().setVisibility(View.GONE);
        if(erro.equals("900003")){
            showToast("没有更多数据了");
        }
    }

    @Override
    public void onLeftClick() {
        finish();
    }


    @Override
    public void onCenterClick() {
    }


    @Override
    public void onRightClick() {

    }

    @Override
    public void onRight2Click() {

    }

    @Override
    public void onItemClick(PrizeHistoryItem data) {

        int lotteryId = data.lotteryType;
        if (lotteryId == LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT || lotteryId == LotteryIDUtils.JSK3_LOTTERY_ID_INT
                || lotteryId == LotteryIDUtils.JLK3_LOTTERY_ID_INT || lotteryId == LotteryIDUtils.GXK3_LOTTERY_ID_INT
                || lotteryId == LotteryIDUtils.CQSSC_LOTTERY_ID_INT || lotteryId == LotteryIDUtils.SD11XUAN5_LOTTERY_ID_INT ||
                lotteryId == LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT || lotteryId == LotteryIDUtils.KLPK_LOTTERY_ID_INT ||
                lotteryId == LotteryIDUtils.KLSF_LOTTERY_ID_INT || lotteryId == LotteryIDUtils.GDKLSF_LOTTERY_ID_INT ||
                lotteryId == LotteryIDUtils.XJ11XUAN5_LOTTERY_ID_INT) {
            return;
        } else {
            Intent intent = new Intent();
            intent.setClass(getContext(), PrizeDetailActivity.class);
            intent.putExtra(IntentParamUtils.LOTTERYID,
                    data.lotteryType);
            intent.putExtra(IntentParamUtils.PERIOD,
                    data.phase);
            intent.putExtra(IntentParamUtils.DATE,
                    data.drawTime);
            intent.putExtra(IntentParamUtils.PRIZENUM,
                    data.wincode);
            startActivity(intent);
        }
    }
}
